﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using CashResource;
using Cash.Properties;
using Cash.Model;

namespace CashCommon
{
    public static class Builder
    {
        public static void Init()
        {
            try
            {
                _asm = AppDomain.CurrentDomain.Load("CashConnect");
                CashAccountGate = _asm.GetType("CashConnect.CashAccountGate").GetProperty("Instance").GetValue(null, null) as Cash.Model.ICashAccountGate;
                CashRegisterGate = _asm.GetType("CashConnect.CashRegisterGate").GetProperty("Instance").GetValue(null, null) as Cash.Model.ICashRegisterGate;
                CashEmployerGate = _asm.GetType("CashConnect.CashEmployerGate").GetProperty("Instance").GetValue(null, null) as Cash.Model.ICashEmployerGate;
                _type = _asm.GetType("CashConnect.FormConnect");
                DB = _asm.GetType("CashConnect.WFSql").GetProperty("DB").GetValue(null, null) as IDatabaseBase;
                SqlGate = Activator.CreateInstance(_asm.GetType("CashConnect.SqlGate")) as ISqlGate;
                TaxesGate = Activator.CreateInstance(_asm.GetType("CashConnect.CashTaxesGate")) as ITaxesGate;
            }
            catch (Exception err)
            {
                throw new ECashUsers(MessageType.AllertLog, CommonError.CashConnectLoadAssemblyError, err);
            }
        }

        #region Poperties
        
        /// <summary>
        /// Возвращается объект класса CashAccountGate
        /// </summary>
        public static Cash.Model.ICashAccountGate CashAccountGate { get; private set; }

        /// <summary>
        /// Возвращается объект класса CashRegisterGate
        /// </summary>
        public static Cash.Model.ICashRegisterGate CashRegisterGate { get; private set; }

        /// <summary>
        /// Возвращается объект класса CashEmployerGate
        /// </summary>
        public static Cash.Model.ICashEmployerGate CashEmployerGate { get; private set; }

        /// <summary>
        /// Возвращается форма коннекции
        /// </summary>
        public static Form FormConnect { get { return Activator.CreateInstance(_type) as Form; } }

        /// <summary>
        /// Возвращается объект соединения с базой данных
        /// </summary>
        public static IDatabaseBase DB { get; private set; }

        /// <summary>
        /// Возвращается объект для работы с Sequensie MSSql
        /// </summary>
        public static ISqlGate SqlGate { get; private set; }

        /// <summary>
        /// Возвращается объект для работы с печатью
        /// </summary>
        public static IPrintDoc PrintDoc(CashRegister cashRegister) 
        {
            if (cashRegister.N_Releaze == 1)
                return new PrintDoc01();
            else
                return new PrintDoc02();
        }

        /// <summary>
        /// Возвращается объект для работы с НДС
        /// </summary>
        public static ITaxesGate TaxesGate { get; private set; }

        #endregion

        #region Private
        private static Assembly _asm;
        private static Type _type;
        #endregion
    }
}
